package cn.shyouchi.wuguanjia.android.ui.event.arrange;

import android.text.Html;
import android.view.View;

import com.chad.library.adapter.base.BaseMultiItemQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.chad.library.adapter.base.entity.MultiItemEntity;

import java.util.List;

import cn.shyouchi.wuguanjia.android.R;
import cn.shyouchi.wuguanjia.android.impl.ModuleItemClickListener;
import cn.shyouchi.wuguanjia.android.impl.OnItemClickListener;
import cn.shyouchi.wuguanjia.android.ui.event.labor.LaborRequestEntity;
import cn.shyouchi.wuguanjia.android.utils.ToastUtil;

/**
 * Created by Eenie on 2016/11/25 at 16:44
 * Email: 472279981@qq.com
 * Des:
 */

public class RankAdapter extends BaseMultiItemQuickAdapter<MultiItemEntity, BaseViewHolder> {

    public static int RANK_TYPE_ITEM = 0;
    public static int RANK_TYPE_SUBITEM = 1;
    OnItemClickListener onItemClickListener;
    ModuleItemClickListener mItemClickListener;


    public RankAdapter(List<MultiItemEntity> data, OnItemClickListener listener,ModuleItemClickListener moduleItemClickListener) {
        super(data);
        addItemType(RANK_TYPE_ITEM, R.layout.item_event_arrange_rank_layout);
        addItemType(RANK_TYPE_SUBITEM, R.layout.item_event_arrange_rank_subitem_layout);
        this.onItemClickListener = listener;
        mItemClickListener = moduleItemClickListener;
    }

    @Override
    protected void convert(final BaseViewHolder holder, final MultiItemEntity entity) {
        switch (holder.getItemViewType()) {
            case 0:
                final LaborRequestEntity.ResultMessageBean.ServiceBeanX service = (LaborRequestEntity.ResultMessageBean.ServiceBeanX) entity;
                holder.setText(R.id.tv_date, String.format("%1s(%d/%2s)", service.getService().getServicesname(), service.getSubItems() == null ? 0 : service.getSubItems().size(), service.getService_people()));
                holder.getView(R.id.img_add).setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {


                        if (service.getSubItems() != null && service.getSubItems().size() >= service.getService_people()) {
                            ToastUtil.showToast(String.format("%1s仅需%2s人", service.getService().getServicesname(), service.getService_people()));
                        } else {
                            onItemClickListener.onItemClick(entity);
                        }

                    }
                });


                holder.itemView.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        int pos = holder.getAdapterPosition();
                        if (service.isExpanded()) {
                            collapse(pos);
                        } else {
                            expand(pos);
                        }
                    }
                });
                break;
            case 1:
                final PersonRankAdapter.Person person = (PersonRankAdapter.Person) entity;

                holder.setText(R.id.tv_line_name, Html.fromHtml(String.format("<u>%1s</u>", person.getLine() != null && person.getLine().getId() != 0 ? person.getLine().getName() : "添加巡检线路")));

                holder.getView(R.id.tv_line_name).setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        mItemClickListener.moduleItemClick(person);
                    }
                });


                holder.setText(R.id.tv_person_name, person.getUser_name());


                holder.getView(R.id.img_del).setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        LaborRequestEntity.ResultMessageBean.ServiceBeanX service = (LaborRequestEntity.ResultMessageBean.ServiceBeanX) person.getTag();
                        service.removeSubItem(person);
                        getData().remove(holder.getAdapterPosition());
                        RankAdapter.this.notifyDataSetChanged();
                        onItemClickListener.onDel(person);
                    }
                });
                break;

        }
    }


    public LaborRequestEntity.ResultMessageBean.ServiceBeanX getServiceByRankId(int rankid) {
        for (MultiItemEntity entity : this.getData()) {
            if (entity instanceof LaborRequestEntity.ResultMessageBean.ServiceBeanX) {
                final LaborRequestEntity.ResultMessageBean.ServiceBeanX service = (LaborRequestEntity.ResultMessageBean.ServiceBeanX) entity;
                if (service.getService().getId() == rankid) {
                    return service;
                }
            }
        }
        return null;
    }


//    public void ExpandItem() {
//        int i = 0;
//        for (MultiItemEntity entity : this.getData()) {
//            if (entity instanceof LaborRequestEntity.ResultMessageBean.ServiceBeanX) {
//                ((LaborRequestEntity.ResultMessageBean.ServiceBeanX) entity).setExpanded(true);
//                expand(i);
//                i++;
//            }
//        }
//    }
}
